package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishListVo;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    @GetMapping("/page")
public Result<PageResult> pageDish(DishPageQueryDTO dishPageQueryDTO){

    PageResult pageResult =dishService.pageDish(dishPageQueryDTO);
return Result.success(pageResult);
}
@PostMapping
@CacheEvict(cacheNames = "dishCache", key = "#dishDTO.categoryId")
    public Result addDishAndFlavor(@RequestBody DishDTO dishDTO){

        dishService.addDishAndFlavo(dishDTO);

        return  Result.success();
}
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result openOrClose(@PathVariable Integer status, Long id){
        dishService.openOrClose(status,id);
        return Result.success();
    }
    @GetMapping("{id}")
    public Result dishById(@PathVariable Long id){
        DishVO dishVO=dishService.dishById(id);

        return Result.success(dishVO);
    }

@PutMapping
@CacheEvict(cacheNames ="dishCache",key = "#dishDTO.categoryId")
    public Result dishUpdate(@RequestBody DishDTO dishDTO){
        dishService.dishUpdate(dishDTO);
return  Result.success();
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public  Result dishDelete(Integer[] ids){
        dishService.dishDelete(ids);
      return   Result.success();
    }
    @GetMapping("/list")
    public Result categoryByIdList( Long categoryId) {
    List<Dish> dishVOS= dishService.categoryByIdList(categoryId);
        System.out.println(dishVOS);

        return Result.success(dishVOS);
    }

}
